package cvo.chess.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;

import cvo.chess.client.entities.GameFacade;
import cvo.chess.client.ui.chessboard.ChessboardWidget;
import cvo.chess.client.ui.movement.MovementControlerWidget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ChessSandbox implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		try {
			
			GameFacade gf = GameFacade.get();

			// First we create a game.
			// In real we only need to create a new game if a new game start,
			// otherwise the user will resume a game
			// In that case we want to find the game in our database...
			gf.newGame();

			// Otherwise something like (pseudo-code):
			// String id = HistoryToken.get('id');
			// gf.getGame(id);

			// TODO: Add player and find opponent

			gf.startGame();

			// Actually, this should be possible before the game start...
			// ...but the binding and updating would be a little bit more
			// complex
			// So for now, we live with it.
			ChessboardWidget cbWidget = new ChessboardWidget(gf);
			MovementControlerWidget movementControlerWidget = new MovementControlerWidget(gf);
			
			RootPanel.get("chessBoardContainer").add(cbWidget);
			RootPanel.get("movementContainer").add(movementControlerWidget);

		} catch (Exception ex) {
			Window.alert(ex.getLocalizedMessage());
		}
	}
}
